let requestUrl = null;
let stateArray = ["未启用", "已启用"];

function addHandler() {
	requestUrl = "/role_chengwei/insert";

	$("#chengwei_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#chengwei_list").datagrid("getSelected");
	
	if (rowData) {
		requestUrl = "/role_chengwei/updateById";

		$("#id").val(rowData.id);
		$("#roleId").combobox("setValue", rowData.roleId);
		$("#endTime").datebox("setValue", rowData.endTime);
		$("#useStatus").combobox("setValue", rowData.useStatus);
		$("#chengweiId").combobox("setValue", rowData.chengweiId);

		$("#chengwei_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "info");
	}
}

/**
 * 批量删除
 */
function deleteHandler() {
	let rows = $("#chengwei_list").datagrid("getChecked");
	let count = rows.length;

	if (count > 0) {
		confirmAction("确认删除勾选的" + count + "条数据？", function() {
			let ids = [];

			for (let i = 0; i < rows.length; i++) {
				let id = rows[i].id;

				ids.push(id);
			}

			/**
			 * 批量删除
			 */
			ajaxPost("/chengwei/deleteByIds", {
				ids: JSON.stringify(ids)
			}, function(resp) {
				showMsg(resp.message);

				$("#chengwei_list").datagrid("reload");
				$("#chengwei_list").datagrid("uncheckAll");
				$("#chengwei_list").datagrid("unselectAll");
			}, error);
		});
	} else {
		alertMsg("请选择要删除的记录！", "warning");
	}
}

$(document).ready(function() {

	$("#role_id").combobox({
		url: "/role_account/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		panelHeight: "auto",
		prompt: "--选择角色--",
		formatter: function(row) {
			ajaxGet("/role_category/selectById", {
				id: row.categoryId
			}, function(resp) {
				let data = resp.data;

				$("#modeling_" + row.id).attr("src", data.image)
					.attr("title", data.name);
			}, error);

			return "<img width='18' id='modeling_" + row.id + "' />&nbsp;" + row.name;
		}
	});

	$("#chengwei_id").combobox({
		url: "/chengwei/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		panelHeight: "auto",
		prompt: "--选择称谓类型--"
	});

	// 点击[搜索]按钮
	$("#search").linkbutton({
		text: "搜索",
		iconCls: "icon-search"
	}).click(function() {
		let roleId = $("#role_id").combobox("getValue");
		let chengweiId = $("#chengwei_id").combobox("getValue");
		
		$("#chengwei_list").datagrid("load", {
			roleId: roleId,
			chengweiId: chengweiId
		});
	});

	$("#clear").linkbutton({
		text: "清空",
		iconCls: "icon-delete"
	}).click(function() {
		$("#search_form").form("clear");
	});

	$("#chengweiId").combobox({
		url: "/chengwei/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		required: true,
		panelHeight: "auto"
	});

	fetchSelect("#useStatus", stateArray, {
		width: 150,
		required: true
	});

	$("#endTime").datebox({
		width: 150,
		showSeconds: true
	});

	fetchRoleId(150);

	$("#chengwei_dialog").dialog({
		title: "角色称谓信息",
		width: 500,
		height: 300,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#chengwei_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#chengwei_dialog").dialog("close");
						$("#chengwei_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#chengwei_dialog").dialog("close");
				$("#chengwei_form").form("clear");
			}
		}]
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_role_details * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	$("#chengwei_list").datagrid({
		url: "/role_chengwei/selectByPage",
		method: "get",
		height: height_table,
		rownumbers: true,
		fitColumns: true,
		pagination: true,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}, "-", {
			iconCls: "icon-delete",
			text: "删除",
			handler: function() {
				deleteHandler();
			}
		}],
		columns: [[
			{field: "ck", checkbox: true},
			{field: "id", title: "编号", align: "center", hidden: true},
			{field: "chengweiId", title: "称谓", align: "center", width: 100
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/chengwei/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#chengwei_" + rowIndex).html(data.name);
					}, error);

					return "<div id='chengwei_" + rowIndex + "'></div>";
				}
			},
			{field: "useStatus", title: "启用状态", align: "center", width: 100
				, formatter: function(value) {
					return "<div>" + stateArray[value] + "</div>";
				}
			},
			{field: "endTime", title: "称谓过期时间", align: "center", width: 100
				, formatter: function(value) {
					if (value) {
						return "<div class='red'>" + value + "</div>";
					}  else {
						return "<span>/</span>";
					}
				}
			},
			roleDetailsColumn
		]]
	});

});